//
//  MYCreditsMallVC.m
//  MuYing
//
//  Created by tbb on 2019/8/9.
//  Copyright © 2019 TBB. All rights reserved.
//
//国际化处理完成
#import "MYCreditsMallVC.h"
#import "MYCreditsExchangeRecordVC.h"
#import "MYCreditsMallToOrderVC.h"
#import "YQOrderManageVC.h"
#define kEXCHANGEBTN_TAG 11100

@interface MYCreditsMallVC ()

@property (strong, nonatomic) NSMutableArray *integerArr;

@property (strong, nonatomic) YQPIntegerModel *integerModel;

@end

@implementation MYCreditsMallVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _setupNav];
    self.navigationItem.title = kYQLS(@"积分商城");
    UIImage *leftImage = YQIMG(@"nav_back_sIcon");
    [self.leftItem setImage:[leftImage qmui_imageWithTintColor:UIColor.whiteColor] forState:(UIControlStateNormal)];
    self.rightItemTitle = kYQLS(@"兑换记录");
    [self.rightItem setTitleColor:UIColor.whiteColor forState:(UIControlStateNormal)];
    
    self.integerArr = NSMutableArray.new;

    [self setupRefresh];
    
   
}

- (void)setupRefresh
{
    self.listTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getUserHomeCollectionsApi:YES];
    }];

    self.listTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        if (self.integerArr.count < 10) {
            [self.listTableView.mj_footer endRefreshing];
            return ;
        }
        self.page ++;

        [self getUserHomeCollectionsApi:NO];
    }];
    [self.listTableView.mj_header beginRefreshing];
    self.listTableView.mj_header.automaticallyChangeAlpha = YES;

}
#pragma amrk - POST查询首页帖子列表
- (void)getUserHomeCollectionsApi:(BOOL)isReload
{
    if (isReload) {
        self.page = 1;
    }
    NSMutableDictionary *dic = NSMutableDictionary.new;

    dic[@"page"] = @(self.page);
    dic[@"limit"] = @(10);

    
    [YQPNetWorkingManager sendPostRequest:kPOST_INTERGER_LIST_URL parameters:dic success:^(YQPNetWorkingBaseModel * _Nonnull responseObject) {
        if (self.page == 1) {
            [self.integerArr removeAllObjects];
        }
        self.integerModel = [YQPIntegerModel mj_objectWithKeyValues:responseObject.data];
        [self.integerArr addObjectsFromArray:self.integerModel.integralGoodsList];
        [self.listTableView reloadData];
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];

    } failure:^(NSUInteger statusCode, NSString * _Nonnull error) {
        [self.listTableView.mj_header endRefreshing];
        [self.listTableView.mj_footer endRefreshing];
    }];
}


-(void)setupView {
    [super setupView];
    kYQREGISTERTABLEVIEWCNIBHeaderFooter(@"MYCreditsMallHeader");
    kYQREGISTERTABLEVIEWNIBCELL(@"MYCreditsMallCell")
}
-(void)rightItemAction:(UIButton *)sender {
    MYCreditsExchangeRecordVC *recordVC = [[MYCreditsExchangeRecordVC alloc] init];
    kYQPUSH(recordVC);
}

-(void)exchangeAction:(UIButton *)sender {
    NSInteger row = sender.tag - kEXCHANGEBTN_TAG;
    YQPIntegerEntityModel *goodsModel = nil;
    if (row < self.integerArr.count) {
        goodsModel = self.integerArr[row];
    }
    MYCreditsMallToOrderVC *orderVC = [[MYCreditsMallToOrderVC alloc] init];
    orderVC.goodsID = goodsModel.integralGoodsId;
    orderVC.isIntegral = YES;
    [self.navigationController pushViewController:orderVC animated:true];
    
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(nonnull UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.integerArr.count;
}
- (nonnull UITableViewCell *)tableView:(nonnull UITableView *)tableView cellForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    YQPIntegerEntityModel *model = nil;
    if (indexPath.row < self.integerArr.count) {
        model = self.integerArr[indexPath.row];
    }
    MYCreditsMallCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MYCreditsMallCell"];
    cell.goodsModel = model;
    cell.exchangeBtn.tag = kEXCHANGEBTN_TAG + indexPath.row;
    [cell.exchangeBtn addTarget:self action:@selector(exchangeAction:) forControlEvents:(UIControlEventTouchUpInside)];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 92;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    MYCreditsMallHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"MYCreditsMallHeader"];
    header.creditsLb.text = [NSString stringWithFormat:@"%@ %@",kYQLS(@"我的积分"),self.integerModel.userIntegral?:@"0"];
    ws(self);
    [header.lookupBtn addTapAction:^(id  _Nonnull sender) {
        MYCreditsMallRuleVC *ruleVC = [[MYCreditsMallRuleVC alloc] initWithNibName:@"MYCreditsMallRuleVC" bundle:nil];
        kYQPUSH(ruleVC);
    }];
    [header.creditsOrderBtn addTapAction:^(id  _Nonnull sender) {
        YQOrderManageVC *VC = [[YQOrderManageVC alloc] init];
        VC.isIntegral = YES;
        [weakself.navigationController pushViewController:VC animated:true];

    }];
    [header.userIcon setImageWithURL:YQSTRINGTOURL([YQPUserTool account].userHead) placeholder:nil];
    return header;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 177;
}
#pragma mark - UITableViewDelegate, UITableViewDataSource end

#pragma -mark 私有
-(void)_setupNav {
    UIFont *font = kSystemMedium(17);
    NSDictionary *dic = @{ NSFontAttributeName:font,NSForegroundColorAttributeName:UIColor.whiteColor};
    self.navigationController.navigationBar.titleTextAttributes =dic;
    // 一行代码搞定导航栏颜色
    [self wr_setNavBarBarTintColor:kAPPThemeColor];
    [self wr_setNavBarTitleColor:[UIColor whiteColor]];
}
@end
